package com.alibaba.web.service.impl;

import com.alibaba.api.pojos.Account;
import com.alibaba.api.pojos.User;
import com.alibaba.api.service.AccountServiceApi;
import com.alibaba.api.service.TradeServiceApi;
import com.alibaba.api.service.UserServiceApi;
import com.alibaba.web.config.UserThreadLocal;
import com.alibaba.web.dtos.RequestDTO;
import com.alibaba.web.dtos.ResponseResult;
import com.alibaba.web.service.WebService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Map;

@Service
public class WebServiceImpl implements WebService {

    @Reference(version = "1.0.0")
    private UserServiceApi userServiceApi;

    @Reference(version = "1.0.0")
    private AccountServiceApi accountServiceApi;

    @Reference(version = "1.0.0")
    private TradeServiceApi tradeServiceApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    private static final User USER = UserThreadLocal.get();

    @Override
    public ResponseResult register(RequestDTO requestDTO) {
//参数检查
        String phone = requestDTO.getPhone();
        String code = requestDTO.getCode();

        if(null == requestDTO || null == code || null == phone)
            return ResponseResult.error(300,"无效参数");

        if (!redisTemplate.hasKey(phone))
            return ResponseResult.error(400,"验证码不存在");

        if (!code.equals(redisTemplate.opsForValue().get(phone)))
            return ResponseResult.error(401,"验证码不正确");

        User user = new User();
        user.setUsername(requestDTO.getName());
        user.setPassword(DigestUtils.md5DigestAsHex(requestDTO.getPassword().getBytes()));
        user.setPhone(requestDTO.getPhone());
        //保存用户
        userServiceApi.register(user);

        redisTemplate.delete(phone);
        //结果集封装
        return ResponseResult.ok(200, "ok");
    }

    @Override
    public ResponseResult login(String phone, String code) {
        //参数检查
        if (null == phone || null == code) {
            return ResponseResult.error(300,"无效参数");
        }

        if (!redisTemplate.hasKey(phone))
            return ResponseResult.error(400,"验证码不存在");

        if (!code.equals(redisTemplate.opsForValue().get(phone)))
            return ResponseResult.error(401,"验证码不正确");

        //获取token
        Map<String, String> map = userServiceApi.login(phone);
        //结果封装

        redisTemplate.delete(phone);
        return new ResponseResult(200, "OK", map);
    }

    @Override
    public ResponseResult code(String phone) {
        if (null == phone) {
            return ResponseResult.error(300,"无效参数");
        }

        if (redisTemplate.hasKey(phone))
            return ResponseResult.error(400,"已经发生过验证码");

        Map<String, String> map = userServiceApi.code(phone);


        return new ResponseResult(200, "OK", map);
    }

    @Override
    public ResponseResult recharge(Double money) {
        boolean flag = accountServiceApi.accountByMoney(USER.getAccountId(), money, 1);
        if (flag) {
            return ResponseResult.ok(200, "ok~");
        }
        return ResponseResult.error(500, "充值异常，请重试~");
    }

    @Override
    public ResponseResult withdrawal(Double money) {
        boolean flag = accountServiceApi.accountByMoney(USER.getAccountId(), money, 2);
        if (flag) {
            return ResponseResult.ok(200, "ok~");
        }
        return ResponseResult.error(500, "充值异常，请重试~");
    }

    @Override
    public ResponseResult findByAccountId() {
        Account account = accountServiceApi.findById(USER.getAccountId());
        return new ResponseResult(200, "ok", account);
    }

    @Override
    public ResponseResult tradeList() {
        tradeServiceApi.findById(USER.getAccountId());
        return null;
    }

    @Override
    public ResponseResult account(Account account) {
        boolean insert = accountServiceApi.insert(account);
        if (insert) {
            return ResponseResult.ok(200, "ok");
        }
        return ResponseResult.ok(400, "错误");
    }


}
